﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using DB = HTComics.Data;

namespace HTComics.Web.Admin
{
    public partial class SalesReport1 : System.Web.UI.Page
    {
        protected int pageSize = 100;
        protected int totalRows;
        protected string html;

        protected string summary_StartDate;
        protected string summary_EndDate;
        protected int summary_TotalOrders;
        protected int summary_TotalQuantity;
        protected double summary_TotalCost;
        protected double summary_TotalSales;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["IsAdmin"] == null || !Convert.ToBoolean(Session["IsAdmin"]))
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadReport();
        }

        private void LoadReport()
        {
            string startDate, endDate;
            DataTable dtReport;

            startDate = Request.QueryString["startDate"];
            endDate = Request.QueryString["endDate"];

            summary_StartDate = DateTime.Parse(startDate).ToString("dd/MM/yyyy");
            summary_EndDate = DateTime.Parse(endDate).ToString("dd/MM/yyyy");

            dtReport = DB.Admin.GetSalesReports(startDate + " 00:00:00.000", endDate + " 23:59:59.999");
            totalRows = dtReport.Rows.Count;
            html = GenerateHtml(dtReport);                
        }

        public string GenerateHtml(DataTable dtReport)
        {
            StringBuilder objHtml = new StringBuilder();
            string alterRowCls = "";
            string tmpOrderCoder = "";
            double price, discount;

            if (dtReport.Rows.Count > 0)
                tmpOrderCoder = dtReport.Rows[0]["OrderCode"].ToString();

            for (int i = 0; i <= dtReport.Rows.Count - 1; i++)
            {
                if (i % 2 == 0)
                    alterRowCls = "reportDetailsRow1";                    
                else
                    alterRowCls = "reportDetailsRow2";


                price = Convert.ToDouble(dtReport.Rows[i]["Price"]);
                discount = Convert.ToDouble(dtReport.Rows[i]["Discount"]);
                price = price - (price * discount / 100);


                objHtml.Append("<tr id='row" + i + "' tag='details' style='display:none;' class='" + alterRowCls + "'>");                
                objHtml.Append("<td>" + Convert.ToDateTime(dtReport.Rows[i]["TimeStamp"]).ToString("dd/MM/yyyy h:m tt") + "</td>");
                objHtml.Append("<td>" + dtReport.Rows[i]["OrderCode"] + "</td>");
                objHtml.Append("<td>" + dtReport.Rows[i]["Title"] + " " + dtReport.Rows[i]["IssueNumber"] + "</td>");
                objHtml.Append("<td>" + dtReport.Rows[i]["Code"] + "</td>");
                objHtml.Append("<td>" + dtReport.Rows[i]["Quantity"] + "</td>");
                objHtml.Append("<td>" + dtReport.Rows[i]["CustomField_1"] + "</td>");
                objHtml.Append("<td>" + dtReport.Rows[i]["Cost"] + "</td>");
                objHtml.Append("<td>" + string.Format("{0:N}", price) + "</td>");
                objHtml.Append("<td>" + dtReport.Rows[i]["Discount"] + "%</td>");
                objHtml.Append("<td>" + dtReport.Rows[i]["FirstName"] + " " + dtReport.Rows[i]["LastName"] + "</td>");
                objHtml.Append("</tr>");

                //Summary Variables
                if (dtReport.Rows[i]["OrderCode"].ToString() != tmpOrderCoder)
                    summary_TotalOrders++;

                summary_TotalQuantity += Convert.ToInt32(dtReport.Rows[i]["Quantity"]);
                summary_TotalSales += Convert.ToDouble(price);
                summary_TotalCost += Convert.ToDouble(dtReport.Rows[i]["Cost"]);
            }            

            return objHtml.ToString();
        }
    }
}